package com.party.admin.biz.workflow;

import com.party.admin.utils.RealmUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.copartner.BrokerageStatusEnum;
import com.party.core.model.copartner.TargetCopartner;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.ProjectBrokerage;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.workflow.BusinessInstance;
import com.party.core.service.crowdfund.IProjectBrokerageService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.workflow.IBusinessInstanceService;
import com.party.core.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 微信服务接口
 * Created by wei.li
 *
 * @date 2017/5/19 0019
 * @time 11:31
 */
@Service
public class BusinessInstanceBizService {

    @Autowired
    private IBusinessInstanceService businessInstanceService;

    @Autowired
    private IProjectBrokerageService projectBrokerageService;

    @Autowired
    private IProjectService projectService;

    /**
     * 发起流程
     *
     * @param brokerage 佣金
     * @param targetCopartner 目标合作商
     */
    @Transactional
    public void saveInstanceBiz(ProjectBrokerage brokerage, TargetCopartner targetCopartner) throws Exception {
        Project project = projectService.get(brokerage.getProjectId());
        MemberGroup currentUser = RealmUtils.getCurrentUser();
        ProjectBrokerage dbBrokerage = projectBrokerageService.findByProjectId(brokerage.getProjectId());
        if (dbBrokerage == null) {
            dbBrokerage = new ProjectBrokerage();
            dbBrokerage.setUpdateBy(currentUser.getId());
            dbBrokerage.setAuthorId(project.getAuthorId());
        }
        dbBrokerage.setCopartnerId(targetCopartner.getPartnerId());
        MyBeanUtils.copyBeanNotNull2Bean(brokerage, dbBrokerage);
        if (StringUtils.isEmpty(dbBrokerage.getId())) {
            projectBrokerageService.insert(dbBrokerage);
        } else {
            projectBrokerageService.update(brokerage);
        }

        BusinessInstance instance = businessInstanceService.findByTargetId(brokerage.getProjectId());
        if (instance == null) {
            instance = new BusinessInstance();
            instance.setTitle(targetCopartner.getRealname() + "的佣金审批");
            instance.setCreateBy(currentUser.getId());
            instance.setUpdateBy(currentUser.getId());
            instance.setStep(1);
            instance.setCheckStatus(BrokerageStatusEnum.ONE_MONEY_AUDITING.getCode()); // 待审核
            instance.setTargetId(brokerage.getProjectId());
            instance.setBrokerage(brokerage.getBrokerage());
            businessInstanceService.insert(instance);
        } else {
            instance.setBrokerage(brokerage.getBrokerage());
            businessInstanceService.update(instance);
        }
    }
}
